package com.example.productmanagerspringboot.controller;

import com.example.productmanagerspringboot.data.entity.Good;
import com.example.productmanagerspringboot.data.vo.GoodsVO;
import com.example.productmanagerspringboot.data.vo.Response;
import com.example.productmanagerspringboot.data.vo.ResponseWithData;
import com.example.productmanagerspringboot.service.GoodService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/good")
@Slf4j
@Api(tags = "服务")
public class GoodController {
    private final GoodService service;

    public GoodController(GoodService service) {
        this.service = service;
    }

    @GetMapping("/list")
    @Operation(summary = "展示服务")
    //http://localhost:8080/api/goods/list?pageNumber=1&pageSize=10
//        @ApiImplicitParams(@ApiImplicitParam(paramType = "body", name = "photo", value = "图片文件", dataTypeClass = MultipartFile.class))
    public ResponseEntity<ResponseWithData<GoodsVO>> getGoods(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(new ResponseWithData<>("success", 200, service.getGoods(pageNumber, pageSize, false)));
    }

    @GetMapping("/service/list")
    @Operation(summary = "展示未下架服务")
    //http://localhost:8080/api/goods/list?pageNumber=1&pageSize=10 在售
    public ResponseEntity<ResponseWithData<GoodsVO>> getInStockGoods(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        return ResponseEntity.ok(new ResponseWithData<>("success", 200, service.getGoods(pageNumber, pageSize, true)));
    }

    @GetMapping("/id/{id}")
    @Operation(summary = "获取指定ID的商品")
    //http://localhost:8080/api/good/0
    public ResponseEntity<ResponseWithData<Good>> getGoodById(@PathVariable("id") int id) {
        return ResponseEntity.ok(new ResponseWithData<>("success", 200, service.getGoodById(id)));
    }

    @PostMapping("/add")
    @Operation(summary = "添加商品")
    public ResponseEntity<Response> addGood(@Validated @RequestBody Good good) {
        log.info("addGood:{}", good);
        Response response = new Response();
        if (!service.addGood(good)) {
            response.setResultCode(500);
            response.setMessage("新增失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        } else {
            response.setResultCode(200);
            response.setMessage("新增成功");
            return ResponseEntity.ok().body(response);
        }
    }

    @PutMapping("/add")
    @Operation(summary = "修改商品")
    public ResponseEntity<Response> editGood(@Validated @RequestBody Good good) {
        log.info("editGood:{}", good);
        Response response = new Response();
        if (!service.editGood(good)) {
            response.setResultCode(500);
            response.setMessage("修改失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        } else {
            response.setResultCode(200);
            response.setMessage("修改成功");
            return ResponseEntity.ok().body(response);
        }
    }

    @DeleteMapping("/delete")
    @Operation(summary = "修改商品")
    public ResponseEntity<Response> deleteGood(@Validated @RequestBody Good good) {
        log.info("deleteGood:{}", good);
        Response response = new Response();
        if (!service.deleteGood(good)) {
            response.setResultCode(500);
            response.setMessage("删除失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        } else {
            response.setResultCode(200);
            response.setMessage("删除成功");
            return ResponseEntity.ok().body(response);
        }
    }
}
